<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" :width="800" destroyOnClose>
    <BasicForm @register="registerForm">
      <template #billTypeSlot="{ model, field }">
        <a-input-search v-model:value="model[field]" placeholder="请选择单据类型" readonly @search="billTypeOpen" />
      </template>
      <template #ruleParamsSlot="{ model, field }">
        <a-form-item>
          <a-input class="ipt" v-model:value="ruleParams.label" disabled />
          <a-input class="ipt" v-model:value="ruleParams.value" placeholder="请输入规则参数值value" @input="setRuleParams" />
        </a-form-item>
      </template>
    </BasicForm>
  </BasicModal>
  <!-- 单据类型 -->
  <billTypeSelectModel @register="billTypeRegModal" @getSelectResult="billTypehandleSuccess" v-bind="getBindValue"></billTypeSelectModel>
</template>

<script lang="ts" setup>
import { ref, computed, unref , reactive } from 'vue';
import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from './fill.rule.data';
import { saveFillRule, updateFillRule } from './fill.rule.api';
import billTypeSelectModel from '/@/views/components/billTypeSelectModel.vue';

//设置标题
const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

// 声明Emits
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);

//表单配置
const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 8 },
});
// 定义规则参数
const ruleParams = reactive({
  label : "prefix",
  value : ""
})

//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {  
  //重置表单
  await resetFields();
  ruleParams.value = "";
  // 设置默认的规则实现类
  setFieldsValue({
    ruleClass:'org.aps.modules.system.rule.BillCodeRule'
  })
  setModalProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    if(data.record.ruleParams) {
      let jsonObj = JSON.parse(data.record.ruleParams);
      console.log("Object.keys(jsonObj)",Object.keys(jsonObj));
      ruleParams.label = Object.keys(jsonObj);
      ruleParams.value = jsonObj[Object.keys(jsonObj)]
    }
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  }
});
const [billTypeRegModal, { openModal: openBillTypeModal }] = useModal();
const getBindValue = Object.assign({}, '');

// 设置规则参数
function setRuleParams() {
  let obj = {};
  obj[ruleParams.label] = ruleParams.value;
  // 设置规则参数value
  setFieldsValue({
    ruleParams:JSON.stringify(obj)
  })
}

//打开单据类型
function billTypeOpen() {
  openBillTypeModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
//单据类型回写
function billTypehandleSuccess(val, values) {
  setFieldsValue({
    billTypeName: val[0].label,
    billTypeId: val[0].value,
  });
}
//表单提交事件
async function handleSubmit() {
  try {
    let formValue = await validate();
    setModalProps({ confirmLoading: true });
    if (isUpdate.value) {
      let allFieldsValue = getFieldsValue();
      // 编辑页面 如果表单没有父级下拉框 则提交时候 validate方法不返该值 需要手动设置
      if (!formValue.parentId && allFieldsValue.parentId) {
        formValue.parentId = allFieldsValue.parentId;
      }
      await updateFillRule(formValue);
    } else {
      await saveFillRule(formValue);
    }
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>

<style lang="less" scoped>
  .ipt {
    width:30%;
    margin-left:20px;
  }
</style>